"""
Project Name : api_automation_framework
File Name : conftest.py
Author : lldhsds
Date : 2024/12/29 21:40
Description: fixtures for api_automation_framework
"""

import pytest
import yaml
from utils.api_client import APIClient


# 读取配置文件
def load_env_config(env_name):
    """
    从 environments.yaml 文件中加载指定环境的配置
    :param env_name: 环境名称 (如 "http_internet")
    :return: 返回协议和基础 URL
    """
    with open("config/environments.yaml", "r") as file:
        config = yaml.safe_load(file)

    # 查找对应的环境配置
    env_config = next((env for env in config['environments'] if env['name'] == env_name), None)

    if not env_config:
        raise ValueError(f"环境 '{env_name}' 未找到")

    return env_config['protocol'], env_config['host']

# client fixture，根据环境配置动态创建 APIClient 实例
@pytest.fixture
def client():
    """
    用于根据配置文件返回一个 APIClient 实例的 Fixture
    """
    def _client(env_name):
        # 加载配置文件中的环境信息
        protocol, host = load_env_config(env_name)
        return APIClient(protocol, host)

    return _client

@pytest.fixture
def auth_credentials():
    """Fixture for authentication credentials."""
    return {
        "user": "testuser",
        "passwd": "testpass",
        "token": "Token123",
        "qop": "auth",
        "algorithm": "SHA256",
        "stale_after": "1"
    }
